package com.second.app.thread.线程基础.day02;

/**
 * @Author GYZ
 * @Date 2023/11/17
 * <pre>
 *   java中 暂停线程意味着此线程可以恢复运行。
 *   suspend()方法暂停线程，resume()方法恢复线程的执行
 *
 * </pre>
 */
public class ZantingThreadDemo {

    public static void main(String[] args) {
        class ZanTingThread extends Thread{
            private long i=0;

            public long getI(){
                return i;
            }

            public void setI(long i){
                this.i = i;
            }

            @Override
            public void run() {
                while (true){
                    i++;
                }
            }
        }

        try {
            ZanTingThread zanTingThread = new ZanTingThread();
            zanTingThread.start();
            Thread.sleep(5000);
            //A
            zanTingThread.suspend();
            System.out.println("A= "+System.currentTimeMillis()+" i="+zanTingThread.getI());
            Thread.sleep(5000);
            System.out.println("A= "+System.currentTimeMillis()+" i="+zanTingThread.getI());
            //B
            zanTingThread.resume();
            Thread.sleep(5000);
            //C
            zanTingThread.suspend();
            System.out.println("B= "+System.currentTimeMillis()+" i="+zanTingThread.getI());
            Thread.sleep(5000);
            System.out.println("B= "+System.currentTimeMillis()+" i="+zanTingThread.getI());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
